package com.irdstudio.efp.ctr.service.impl.daybat;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.console.service.facade.PubSysInfoService;
import com.irdstudio.efp.ctr.service.dao.CtrLoanContBakDao;
import com.irdstudio.efp.ctr.service.facade.BatCtrLoanContBakService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.BaseConstant;
import com.irdstudio.basic.framework.core.util.TimeUtil;

/**
 * <p>
 * ServiceImpl:
 * <p>
 * 
 * @author chenqm1
 * @date 2018-05-04
 */
@Service("batCtrLoanContBakService")
public class BatCtrLoanContBakServiceImpl implements BatCtrLoanContBakService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(BatCtrLoanContBakServiceImpl.class);

	@Autowired
	private CtrLoanContBakDao ctrLoanContBakDao;

	@Autowired
	@Qualifier("pubSysInfoService")
	private PubSysInfoService pubSysInfoService;

	@Override
	public void bakCtrLoanCont() throws Exception {
		logger.info("日终批量任务：备份贷款合同开始......");
		try {
			String openDay = pubSysInfoService.getLastOpenday(BaseConstant.SYS_ID_1001);
			logger.info("系统营业日期：" + openDay);

			String befDate = TimeUtil.addDay(openDay, -7);

			logger.info("贷款合同开始删除备份日期为【" + befDate + "】的数据......");
			Map<String, Object> params = new HashMap<>();
			params.put("bakDate", befDate);
			params.put("openday", openDay);
			ctrLoanContBakDao.deleteByBakDate(params);

			// 支持重复执行，先删除当天备份的数据
			logger.info("贷款合同开始删除备份日期为【" + openDay + "】的数据......");
			params.put("bakDate", openDay);
			ctrLoanContBakDao.deleteByBakDate(params);

			logger.info("贷款合同开始备份数据，备份日期【" + openDay + "】......");
			ctrLoanContBakDao.batchInsertCtrLoanContBak(params);

			logger.info("日终批量任务：备份贷款合同结束......");
		} catch (Exception e) {
			logger.error("贷款合同备份失败！", e);
			throw new Exception("贷款合同备份失败！", e);
		}
	}

	@Override
	public void useLoanAmtDateLimit() throws Exception {
	}

}